<?php
/**
 * Created by PhpStorm.
 * User: zc
 * Date: 2019/8/20
 * Time: 18:50
 */
namespace app\common\logic;

use think\Db;

class SearchLogic
{
    private $org_cate_id = 0;
    private $kwd = '';
    private $page = 1;
    private $pagesize = 15;

    //课程搜搜
    public function getList()
    {
        $courseLog = new CourseLogic();
        $where['name'] = ['like', '%'.$this->kwd.'%'];
        $where['is_chat'] = 0;
        $data = $courseLog->getList($where, $this->pagesize);

        if (Db::name('search_history')->where([
            'user_id'=> UID,
            'name' => $this->kwd,
            'org_cate_id' => $this->org_cate_id
        ])->count() <= 1) {
            //每次搜索 记录一下搜索内容
            Db::name('search_history')->insert([
                'user_id'=> UID,
                'name' => $this->kwd,
                'createtime' => time(),
                'org_cate_id' => $this->org_cate_id
            ]);
        }
        return $data;
    }

    //获取热门搜索
    public function getHot() {
        $where['org_cate_id'] = $this->org_cate_id;
        $where['status'] = 0;
        $list = Db::name('search')->where($where)->select();
        return $list;
    }

    public function getHistory() {
        $where['org_cate_id'] = $this->org_cate_id;
        $where['user_id'] = UID;
        $list = Db::name('search_history')->where($where)->group('name')->limit(15)->order('createtime desc')->select();
        return $list;
    }
    /**
     * @param $org_cate_id
     * @return $this
     */
    public function setOrgCateId($org_cate_id)
    {
        $this->org_cate_id = $org_cate_id;
        return $this;
    }

    /**
     * @return int
     */
    public function getOrgCateId()
    {
        return $this->org_cate_id;
    }

    /**
     * @param $kwd
     * @return $this
     */
    public function setKwd($kwd)
    {
        $this->kwd = $kwd;
        return $this;
    }

    /**
     * @return string
     */
    public function getId(){
        return $this->kwd;
    }

    /**
     * @param $page
     * @return $this
     */
    public function setPage($page)
    {
        $this->page = abs($page) > 0?abs($page):1;
        return $this;
    }

    /**
     * @return int
     */
    public function getPage(){
        return $this->page;
    }

    /**
     * @param $pagesize
     * @return $this
     */
    public function setPagesize($pagesize)
    {
        $this->pagesize = abs($pagesize) > 0?abs($pagesize):15;
        return $this;
    }

    /**
     * @return int
     */
    public function getPagesize(){
        return $this->pagesize;
    }
}